//
//  StatusCell.swift
//  3月5的项目
//
//  Created by 哦哦 on 16/3/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit


class StatusCell: UITableViewCell {
    //声明底部toolbar顶部约束属性对象
    var bottomViewTopConstraint: Constraint?
    

     //模型数据
    var status : Status? {
        didSet {
            //绑定模型数据
//            originalView.status = status
            originalView.status = status
            //卸载约束对象
            bottomViewTopConstraint?.uninstall()
            //如果是转发微博的话 就给转发微博数据模型赋值
            if let re = status?.retweeted_status {
                 //如果是转发微博的话 就给转发微博数据模型赋值
                retweetedView.retweetedStatus = re
                //修改约束  相对于转发微博
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraint =  make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                //显示转发微博
                retweetedView.hidden = false
            }else {
                //cell的复用 解除原理 在一个分支内设置了数据,一定在else 分支内 设置其他数据才能够解除cell的复用
                retweetedView.retweetedStatus = nil
                //相对于原创微博
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraint =  make.top.equalTo(originalView.snp_bottom).constraint
                })
                //隐藏转发微博视图
                retweetedView.hidden = true

            }

        }
    }
    
    
    
   //重写注册  cell
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //设置选中状态
        selectionStyle = .None
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置UI
    private func setupUI() {
        //添加子视图
//        backgroundColor = UIColor.yellowColor()
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        
        
        //设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //测试高度
//            make.height.equalTo(100)
        }
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(originalView)
        }

        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
//            make.top.equalTo(originalView.snp_bottom)
//             make.top.equalTo(retweetedView.snp_bottom)
            self.bottomViewTopConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint

            make.height.equalTo(36)
        }
        //开启自动布局之后 所有的frame布局就会自动失效
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }

    }
    
    
     //原创微博视图
    private lazy var originalView: StatusOriginalView = StatusOriginalView()
    //微博底部视图
    private lazy var bottomView: StatusBottomView = StatusBottomView()
    //转发微博
    private lazy var retweetedView: StatusRetweetedView = StatusRetweetedView()

    
    
    
}
//从xib  / sb 中加载的cell 才会执行这个方法
//override func awakeFromNib() {
//    super.awakeFromNib()
//    // Initialization code
//}
